package io.github.talelin.latticy.controller.v1.K;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.github.talelin.autoconfigure.exception.NotFoundException;
import io.github.talelin.core.annotation.GroupRequired;
import io.github.talelin.core.annotation.LoginRequired;
import io.github.talelin.core.annotation.PermissionMeta;
import io.github.talelin.latticy.common.mybatis.Page;
import io.github.talelin.latticy.common.util.PageUtil;
import io.github.talelin.latticy.dto.K.BExceptionDTO;
import io.github.talelin.latticy.model.K.BExceptionDO;
import io.github.talelin.latticy.model.K.BExceptionDetailDO;
import io.github.talelin.latticy.service.K.BExceptionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.github.talelin.latticy.vo.CreatedVO;
import io.github.talelin.latticy.vo.DeletedVO;
import io.github.talelin.latticy.vo.PageResponseVO;
import io.github.talelin.latticy.vo.UpdatedVO;

import javax.validation.constraints.Min;
import javax.validation.constraints.Max;
import javax.validation.constraints.Positive;

import java.util.List;

/**
 * 状态：0、未确认；1、已确认前端控制器
 *
* @author generator@TaleLin
* @since 2021-06-08
*/
@RestController
@RequestMapping("/v1/BException")
public class BExceptionController {

    @Autowired
    private BExceptionService BExceptionService;
    
    @PostMapping("")
    public CreatedVO create() {
        return new CreatedVO();
    }

    @PutMapping("/{id}")
    @PermissionMeta("更新")
    @GroupRequired
    public UpdatedVO update(@RequestBody @Validated BExceptionDTO dto,
                            @PathVariable @Positive(message = "{id.positive}") Long id) {
        BExceptionService.update(dto, id);
        return new UpdatedVO();
    }
    
    @DeleteMapping("/{id}")
    public DeletedVO delete(@PathVariable @Positive(message = "{id.positive}") Integer id) {
        return new DeletedVO();
    }

    @GetMapping("/{id}")
    public BExceptionDO get(@PathVariable(value = "id") @Positive(message = "{id.positive}") Integer id) {
        BExceptionDO b_data = BExceptionService.getById(id);
        if (b_data == null) {
            throw new NotFoundException("b_gateway not found", 10022);
        }
        return b_data;
    }

    @GetMapping("/page")
    public PageResponseVO<BExceptionDO> page(
            @RequestParam(name = "count", required = false, defaultValue = "10")
            @Min(value = 1, message = "{page.count.min}")
            @Max(value = 30, message = "{page.count.max}") Integer count,
            @RequestParam(name = "page", required = false, defaultValue = "0")
            @Min(value = 0, message = "{page.number.min}") Integer page,
            @RequestParam(name = "StartTime") String StartTime,
            @RequestParam(name = "EndTime") String EndTime,
            @RequestParam(name = "dwmc") String dwmc,
            @RequestParam(name = "bgateway_name") String bgateway_name,
            @RequestParam(name = "collector_name") String collector_name,
            @RequestParam(name = "state") String state,
            @RequestParam(name = "operation") String operation,
            //垃圾
            @RequestParam(name = "dwmc1") String dwmc1,
            @RequestParam(name = "bgateway_name1") String bgateway_name1,
            @RequestParam(name = "collector_name1") String collector_name1,
            @RequestParam(name = "operation1") String operation1,
            @RequestParam(name = "dwmc2") String dwmc2,
            @RequestParam(name = "bgateway_name2") String bgateway_name2,
            @RequestParam(name = "collector_name2") String collector_name2,
            @RequestParam(name = "operation2") String operation2,
            @RequestParam(name = "dwmc3") String dwmc3,
            @RequestParam(name = "bgateway_name3") String bgateway_name3,
            @RequestParam(name = "collector_name3") String collector_name3,
            @RequestParam(name = "operation3") String operation3,
            @RequestParam(name = "dwmc4") String dwmc4,
            @RequestParam(name = "bgateway_name4") String bgateway_name4,
            @RequestParam(name = "collector_name4") String collector_name4,
            @RequestParam(name = "operation4") String operation4
    ) {
        Page<BExceptionDO> pager = new Page<>(page, count);
        QueryWrapper<BExceptionDO> wrapper = new QueryWrapper<>();
        if(StartTime != ""){
            wrapper.ge("collector_time",StartTime);
        }
        if(EndTime != ""){
            wrapper.le("collector_time",EndTime);
        }
//        if(dwmc != ""){
//            wrapper.eq("dwmc",dwmc);
//        };
//        if(bgateway_name != ""){
//            wrapper.eq("b_gateway_name",bgateway_name);
//        };
//        if(collector_name != ""){
//            wrapper.eq("collector_name",collector_name);
//        };
        if(state != ""){
            wrapper.eq("state",state);
        };
//        if(operation != ""){
//            wrapper.eq("operation",operation);
//        };

//        if(dwmc1 != ""){
//            wrapper.or().eq("dwmc",dwmc1);
//        };
//        if(bgateway_name1 != ""){
//            wrapper.or().eq("b_gateway_name",bgateway_name1);
//        };
//        if(collector_name1 != ""){
//            wrapper.or().eq("collector_name",collector_name1);
//        };
//        if(operation1 != ""){
//            wrapper.or().eq("operation",operation1);
//        };
//        if(dwmc2 != ""){
//            wrapper.or().eq("dwmc",dwmc2);
//        };
//        if(bgateway_name2 != ""){
//            wrapper.or().eq("b_gateway_name",bgateway_name2);
//        };
//        if(collector_name2 != ""){
//            wrapper.or().eq("collector_name",collector_name2);
//        };
//        if(operation2 != ""){
//            wrapper.or().eq("operation",operation2);
//        };
//        if(dwmc3 != ""){
//            wrapper.or().eq("dwmc",dwmc3);
//        };
//        if(bgateway_name3 != ""){
//            wrapper.or().eq("b_gateway_name",bgateway_name3);
//        };
//        if(collector_name3 != ""){
//            wrapper.or().eq("collector_name",collector_name3);
//        };
//        if(operation3 != ""){
//            wrapper.or().eq("operation",operation3);
//        };
//        if(dwmc4 != ""){
//            wrapper.or().eq("dwmc",dwmc4);
//        };
//        if(bgateway_name4 != ""){
//            wrapper.or().eq("b_gateway_name",bgateway_name4);
//        };
//        if(collector_name4 != ""){
//            wrapper.or().eq("collector_name",collector_name4);
//        };
//        if(operation4 != ""){
//            wrapper.or().eq("operation",operation4);
//        };
        if(dwmc != "") {
            wrapper.and(i -> i.eq("dwmc", dwmc)
                    .or().eq("dwmc", dwmc1)
                    .or().eq("dwmc", dwmc2)
                    .or().eq("dwmc", dwmc3)
                    .or().eq("dwmc", dwmc4));
        }
        if(bgateway_name != "") {
            wrapper.and(i -> i.eq("b_gateway_name", bgateway_name)
                    .or().eq("b_gateway_name", bgateway_name1)
                    .or().eq("b_gateway_name", bgateway_name2)
                    .or().eq("b_gateway_name", bgateway_name3)
                    .or().eq("b_gateway_name", bgateway_name4));
        }
        if(collector_name != "") {
            wrapper.and(i -> i.eq("collector_name", collector_name)
                    .or().eq("collector_name", collector_name1)
                    .or().eq("collector_name", collector_name2)
                    .or().eq("collector_name", collector_name3)
                    .or().eq("collector_name", collector_name4));
        }
        if(operation != "") {
            wrapper.and(i -> i.eq("operation", operation)
                    .or().eq("operation", operation1)
                    .or().eq("operation", operation2)
                    .or().eq("operation", operation3)
                    .or().eq("operation", operation4));
        }
        IPage<BExceptionDO> paging = BExceptionService.getBaseMapper().selectPage(pager, wrapper);
        return PageUtil.build(paging);
    }

    @GetMapping("/search")
    public PageResponseVO<BExceptionDO> search(
            @RequestParam(name = "count", required = false, defaultValue = "10")
            @Min(value = 1, message = "{page.count.min}") Integer count,
            @RequestParam(name = "page", required = false, defaultValue = "0")
            @Min(value = 0, message = "{page.number.min}") Integer page,
            @RequestParam(name = "keyword") String keyword,
            @RequestParam(name = "StartTime") String StartTime,
            @RequestParam(name = "EndTime") String EndTime,
            @RequestParam(name = "dwmc") String dwmc,
            @RequestParam(name = "bgateway_name") String bgateway_name,
            @RequestParam(name = "collector_name") String collector_name,
            @RequestParam(name = "state") String state,
            @RequestParam(name = "operation") String operation,
            //垃圾
            @RequestParam(name = "dwmc1") String dwmc1,
            @RequestParam(name = "bgateway_name1") String bgateway_name1,
            @RequestParam(name = "collector_name1") String collector_name1,
            @RequestParam(name = "operation1") String operation1,
            @RequestParam(name = "dwmc2") String dwmc2,
            @RequestParam(name = "bgateway_name2") String bgateway_name2,
            @RequestParam(name = "collector_name2") String collector_name2,
            @RequestParam(name = "operation2") String operation2,
            @RequestParam(name = "dwmc3") String dwmc3,
            @RequestParam(name = "bgateway_name3") String bgateway_name3,
            @RequestParam(name = "collector_name3") String collector_name3,
            @RequestParam(name = "operation3") String operation3,
            @RequestParam(name = "dwmc4") String dwmc4,
            @RequestParam(name = "bgateway_name4") String bgateway_name4,
            @RequestParam(name = "collector_name4") String collector_name4,
            @RequestParam(name = "operation4") String operation4
    ) {
        Page<BExceptionDO> pager = new Page<>(page, count);
        //垃圾
        IPage<BExceptionDO> paging = BExceptionService.seachBException(pager,keyword,StartTime,EndTime,dwmc,bgateway_name,collector_name,state,operation,dwmc1,
                bgateway_name1,
                collector_name1,
                operation1,
                dwmc2,
                bgateway_name2,
                collector_name2,
                operation2,
                dwmc3,
                bgateway_name3,
                collector_name3,
                operation3,
                dwmc4,
                bgateway_name4,
                collector_name4,
                operation4);
        return PageUtil.build(paging);
    }

    @GetMapping("/{id}/detail")
    @LoginRequired
    public BExceptionDetailDO getDetail(@PathVariable(value = "id") @Positive Long id) {
        BExceptionDetailDO detail = this.BExceptionService.getDetail(id);
        return detail;
    }

    @GetMapping("/getBException")
    @LoginRequired
    public List<BExceptionDetailDO> getBException() {
        List<BExceptionDetailDO> getBExceptions = this.BExceptionService.getBException();
        return getBExceptions;
    }

    @GetMapping("/AllConfirmed")
    @LoginRequired
    public void AllConfirmedBException() {
        this.BExceptionService.AllConfirmed();
    }

    @GetMapping("/{id}/confirm")
    @LoginRequired
    public BExceptionDO confirm(
            @PathVariable(value = "id")
            @Positive(message = "{id.positive}") Integer id) {
        BExceptionDO confirm = this.BExceptionService.confirm(id);
        return confirm;
    }
}
